﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZDSoft.LMS.Service;
using NHibernate.Criterion;
using ZDSoft.LMS.Domain;
using ZDSoft.LMS.Manager;

namespace ZDSoft.LMS.Component
{
    public class RoleComponent:BaseComponent<Role,RoleManager>,IRoleService
    {
        /// <summary>
        /// 配置权限
        /// </summary>
        /// <param name="id">角色的ID</param>
        /// <param name="systemfunction">所配置功能的ID组成的字符串</param>
        public void Authorize(int id, string systemfunction)
        {
            Role role = manager.Get(id);
            IList<string> systemStr = systemfunction.Split(',');
            IList<SystemFunction> sysList = new SystemFunctionManager().GetAll();
            role.SystemFunctions = null;
            manager.Update(role);
            role.SystemFunctions = new List<SystemFunction>();
            foreach (string sys in systemStr)
            {
                for (int i = 0; i < sysList.Count; i++)
                {
                    string functionID = "tree" + sysList[i].ID;
                    if (functionID == sys)
                    {
                        role.SystemFunctions.Add(sysList[i]);
                        GetParent(sysList[i], role.SystemFunctions);
                    }
                }
            }
            role.SystemFunctions = GetDistinctFunction(role.SystemFunctions);
            manager.Update(role);
        }

        /// <summary>
        /// 得到该功能的父级
        /// </summary>
        /// <param name="function">当前功能</param>
        /// <param name="list">当前角色所拥有的功能的集合</param>
        private void GetParent(SystemFunction function, IList<SystemFunction> list)
        {
            if (function.Parent != null)
            {
                SystemFunction system = new SystemFunctionManager().Get(function.Parent.ID);
                list.Add(system);
                GetParent(system, list);
            }
        }

        /// <summary>
        /// 除去重复的功能
        /// </summary>
        /// <param name="list">功能集合</param>
        /// <returns></returns>
        private IList<SystemFunction> GetDistinctFunction(IList<SystemFunction> list)
        {
            IList<SystemFunction> function = new List<SystemFunction>();
            foreach (SystemFunction system in list)
            {
                if (function.Count == 0)
                {
                    function.Add(system);
                }
                else
                {
                    int count = 0;
                    for (int i = 0; i < function.Count; i++)
                    {
                        if (system.ID == function[i].ID)
                        {
                            count++;
                        }
                    }
                    if (count == 0)
                    {
                        function.Add(system);
                    }
                }
            }
            return function;
        }
    }
}
